"""
FIVEDASH(TM) -- General Purpose Accounting Software
Copyright (C) 2008  Obol Software Pty Ltd

This program is free software: you can redistribute it and/or modify
it. For terms and conditions, see fivedash/license/license.txt

"""

from mod_python import apache
from mod_python import Session
from mod_python import util

from phunc.template import Template as  Template

def genErrorPage(errmsg = '10002: Error generating error page', errurl = '/bin/login.py/login_out',err_code=None):
	if err_code:
		errmsg=showErr(int(err_code))
	content = {
		'error_msg':errmsg,
		'error_url':errurl,		
	}
	template = Template()
	template.parseString(err_page)
	page = template.render(content)
	return page

err_page = '''
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html  xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<style type="text/css">
	body{
		font-family:Arial,sans-serif,DejaVuSans;
		font-size:12px;
		line-height:18px;
		margin:135px auto; 
		padding:0pt;
		width:750px;		
	}
	.error{
		width:100%;
		background:url(/error.gif) no-repeat left 0; 
	}
	.error a{
		color:#fe0000;
		}
	.error a:link,.error a:visited{ 
		text-decoration:underline;
		}
	.error a:hover,.error a:active { 
		text-decoration:none;
		}
	.error div{ 
		margin-left:113px;
		}
	.line-d{ 
		font-weight:bold; 
		font-size:13px; 
		border-bottom:1px solid #8D8D8D; 
		padding-bottom:15px; 
		margin-bottom:15px; 
		width:95%;
		}
	.line-r{ 
		border:2px solid #fe0000; 
		padding:14px; 
		margin:22px 0;
		color:#ff0000;
		}
	</style>
	<title>Fivedash</title>
</head>

<body>
	<div class="error">
		<div class="line-d">Error</div>
		<div class="line-r">{{error_msg}}</div>
		<div><img src="/error02.gif" align="middle" /><a href="{{error_url}}">Continue</a></div>
	</div>
</body>
</html>
'''

error_map={
	10000: 'Application',
	10001: 'User not logged in',
	10002: 'Error generating error page',
	10003: 'User has no right on any company.Please contact system Administator',
	10004: 'The UserGroup has no right.Please contact system Administator',
	10005: 'You have not enough to access this url',
	20000: 'Operations portal',
	20100: 'Customer created',
	20199: 'Error creating customer',
	20200: 'Purchase order created',
	20299: 'Error creating purchase order',
	20300: 'Payment recorded',
	20399: 'Error recording bill payment',
	30000: 'Finance portal',
	30100: 'Account created',
	30101: 'Duplicate account name or id',
	30199: 'Error creating account',
	30200: 'Invoice created',
	30299: 'Error creating invoice',
	30300: 'Journal created',
	30301: 'Journal does not balance',
	30302: 'Journal does not exist',
	30399: 'Error creating journal',
	30400: 'Journal posted',
	30499: 'Error posting journal',
	30500: 'Journal updated',
	30599: 'Error updating journal',
	30600: 'Subledger created',
	30699: 'Error creating subledger',
	30900: 'Invoices printed',
	30999: 'Error printing invoices',
	31000: 'Expense report created',
	31099: 'Error creating expense report',
	31100: 'Receipt created',
	31199: 'Error creating receipt',
	40000: 'HR portal',
	50000: 'CRM portal',
	50100: 'Contact created',
	50199: 'Error creating contact',
	50200: 'Contact updated',
	50299: 'Error updating contact',
        50300: 'Contact deleted', 
        50399: 'Error deleting contact', 
        50400: 'Contact merged', 
        50499: 'Error merging contact', 
	60000: 'Admin portal',
	70000: 'Database',
	80000: 'Server',
	99999: 'Unknown Error',
}
def showErr(id):
        if error_map.has_key(id):
                return str(id) + ': ' + error_map[id]
        else:
                return str(id) + ': Unlisted error (no description available)'
